package com.example.datastructures.util;

import com.example.datastructures.TreeNode;


public abstract class TreeUtil {
	
	public static int size(TreeNode treeNode){
		
		if (treeNode != null){
			return size(treeNode.getLeft()) + size(treeNode.getRight());
		} else {
			return 1;
		}
	}
	
	public static boolean searchValue(TreeNode treeNode, int value){
		if (treeNode != null){
			
			if (treeNode.getValue() == value){
				return true;
			} else if (treeNode.getValue() < value){
				return searchValue(treeNode.getRight(), value);
			} else {
				return searchValue(treeNode.getRight(), value);
			}
		} else {
			return false;
		}
	}
	
	public static void insertNode(TreeNode treeNode, int value){
		
		if (value > treeNode.getValue()){
			
			if (treeNode.getRight() == null){
				treeNode.setRight(new TreeNode(value, null, null));
			} else {
				insertNode(treeNode.getRight(), value);
			}
		} else {
			
			if (treeNode.getLeft() == null){
				treeNode.setLeft(new TreeNode(value, null, null));
			} else {
				insertNode(treeNode.getLeft(), value);
			}
		}
		
	}
	
	public static int searchMinValueA(TreeNode treeNode){
		
		if (treeNode.getLeft() != null){
			return searchMinValueA(treeNode.getLeft());
		} else {
			return treeNode.getValue();
		}
	}
	
	public static void printNodes(TreeNode treeNode){
		if (treeNode != null){
			
			printNodes(treeNode.getRight());
			printNodes(treeNode.getLeft());
			System.out.println(treeNode.getValue());
		}
	}
	
	public static boolean sameTree(TreeNode treeNodeA, TreeNode treeNodeB){
		if ((treeNodeA != null) && (treeNodeB != null)){
			
			if (treeNodeA.getValue() == treeNodeB.getValue()){
				 
				if (sameTree(treeNodeA.getRight(), treeNodeB.getRight())){
					return sameTree(treeNodeA.getLeft(), treeNodeB.getLeft());
				} else {
					return false;
				}
				
			} else {
				return false;
			}
		} else {
			
			return treeNodeA == null && treeNodeB == null;
		}
	}
	public static void main(String... args){
		TreeNode node = null;
		TreeNode nodeB = null;
		
		node = new TreeNode(10, null, null);
		insertNode(node, 5);
		insertNode(node, 15);
		insertNode(node, 6);
		insertNode(node, 2);
		insertNode(node, 1);
		insertNode(node, 0);
		insertNode(node, 3);
		
		
		nodeB = new TreeNode(10, null, null);
		insertNode(nodeB, 5);
		insertNode(nodeB, 15);
		insertNode(nodeB, 6);
		insertNode(nodeB, 2);
		insertNode(nodeB, 1);
		insertNode(nodeB, 0);
		insertNode(nodeB, 3);
		
		
		System.out.println("Tree Size: " + searchValue(node, 6));
		System.out.println("Min Value: " + searchMinValueA(node));
		System.out.print("Tree Values: ");
		printNodes(node);
		
		System.out.println(sameTree(node, nodeB));
	}
}